

"""
These programs should be rew written in C, but sometimes I write them in Python since it's much easier to do so.
"""

#from the paper Inferring Min and Max Invariants using Max-plus Polyhedra by Xavier Allamigeon
import random

def mylen(n,s):
    try:
        return s.index('\0')
    except ValueError:
        return randint(n,len(s)-1)

def make_rand_list(llen):
    rs = list(' '*llen)
    null_idx = randint(0,len(rs)-1)
    rs[null_idx] = '\0'
    #print 'null_idx', null_idx
    return rs

def strcopy(n):

    llen = 20
    src = make_rand_list(llen+1)
    dst = make_rand_list(llen+1)

    assert 0 <= len(src) and n <= len(src);
    assert 0 <= len(dst) and n <= len(dst);    
    
    i = 0

    while i <= n-1:
        dst[i]=src[i]
        i = i+1

    print 'nn s d'
    print n,mylen(n,src),mylen(n,dst)

    #inv
    #n < len(src) => len(dst) >= n
    #n = len(src) => len(dst) == len(src)




def strncopy(n):
    
    llen = 20
    src = make_rand_list(llen+1)
    dst = make_rand_list(llen+1)

    assert 0 <= len(src) and n <= len(src);
    assert 0 <= len(dst) and n <= len(dst);    


    i = 0
    while i < n and src[i] != '\0':
      dst[i] = src[i];
      i = i+1


    while i < n:
      dst[i] = '\0';
      i = i+1


    print 'nn s d'
    print n,mylen(n,src),mylen(n,dst)



def mysorted(len=3):
    pass

#Deepak session example from Qi Lu
def f(x,y):
    print 'x y'
    while x < 10:
        if x < 5:
            x = x+1
        else:
            x = x+1
            y = y+1
        print x,y


# if __name__ == '__main__':
#     f(0,5)

